package com.blast.comicsisland.adapters;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;

import android.widget.ImageView;
import android.widget.TextView;
import com.blast.comicsisland.R;
import com.blast.comicsisland.model.recommend.recommendbody.ComicsList;

import java.util.List;

import com.blast.comicsisland.utils.XUtilsImageLoader;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import kale.adapter.AdapterItem;
import kale.adapter.ViewHolder;
import kale.recycler.ExCommonRcvAdapter;

/**
 * Created by ASUS on 2015/6/25.
 */
public class WaterFallAdapter extends ExCommonRcvAdapter<ComicsList>{
    XUtilsImageLoader xUtilsImageLoader;
    BitmapDisplayConfig bigPicDisplayConfig;
    public WaterFallAdapter(List<ComicsList> data,Context context) {
        super(data);
        xUtilsImageLoader = new XUtilsImageLoader(context);
        bigPicDisplayConfig = new BitmapDisplayConfig();
        bigPicDisplayConfig.setBitmapConfig(Bitmap.Config.RGB_565);

    }

    @NonNull
    @Override
    protected AdapterItem<ComicsList> initItemView(Object o) {
        return new WaterFallNormalItem();
    }

    /**
     * 图片实际Item 类似ViewHolder
     */
    public class WaterFallNormalItem implements AdapterItem<ComicsList>{
        //fresco图片空间
        //public SimpleDraweeView imageView;

        public ImageView imageView;
        public TextView title;
        public TextView partName;
        public Context context;


        @Override
        public int getLayoutResId() {
            return R.layout.waterfall_item;
        }

        @Override
        public void initViews(ViewHolder viewHolder, ComicsList comicsList, int i) {
            //fresco图片加载时高度是随机的 这样可以实现瀑布流 但高度不是原图高度
        /*imageView = viewHolder.getView(R.id.waterfall_imageview);
        String format = String.format("%.2f", Math.random() + 1);
        imageView.setAspectRatio(Float.parseFloat(format));*/
            imageView = viewHolder.getView(R.id.waterfall_imageview);
            title = viewHolder.getView(R.id.waterfall_title);
            partName = viewHolder.getView(R.id.waterfall_partname);
            setViews(comicsList,i);

        }

        private void setViews(ComicsList comicsList,int position){
            //TODO imageView 网络加载
            //1.ImageLoaderTask加载 速度慢 且需要setTag防止图片错乱
            //imageView.setTag(comicsList.getCoverurl());
            //new ImageLoadTask(imageView).execute(comicsList.getCoverurl());
            //2.fresco加载 支持渐进式图片加载 但高度需要固定 速度快 且自动释放内存
            //Uri uri = Uri.parse(comicsList.getCoverurl());
            //imageView.setImageURI(uri);
            //3.xUtils加载图片 支持瀑布流 支持快速滚出屏幕的图片不去加载 支持自动释放内存

            xUtilsImageLoader.display(imageView, comicsList.getCoverurl());

            title.setText(comicsList.getBigbookName());
            partName.setText(comicsList.getSubjectName());
        }
    }
}
